//
//  NetWorkCall.m
//  NetWorkManager
//
//  Created by FeiXiang on 16/12/8.
//  Copyright © 2016年 飞翔. All rights reserved.
//

#import "NetWorkCall.h"
#import "SessionManager.h"
#import "Ability.h"
#import "ConfigData.h"
#import "MBProgressHUD.h"

@implementation NetWorkCall

+(void)getRequestWithURL:(NSString *)urlString param:(NSDictionary *)param success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    if (![Ability hasNet]) {
        
        [[[UIAlertView alloc]initWithTitle:NETTITLE message:NETCONTENT delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
        return;
    }
    //为了防止出现中文在这里需要做一下处理
    urlString =[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    AFHTTPSessionManager * manager =[ SessionManager sharedManager];
    
    [manager GET:urlString parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
       
        failure(error);
        
    }];
    
}

+(void)postRequestWithURL:(NSString *)urlString param:(NSDictionary *)param success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure
{
    if (![Ability hasNet]) {
        
        [[[UIAlertView alloc]initWithTitle:NETTITLE message:NETCONTENT delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
        return;
    }
    urlString =[ urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPSessionManager * manager =[ SessionManager sharedManager];
    
    [manager POST:urlString parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (success) {
            
            success(responseObject);
        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        
      
    }];
    
}

+(void)uploadImageWithArray:(NSArray *)imageArray url:(NSString *)urlString fileName:(NSString *)fileName names:(NSArray *)namesArray param:(NSDictionary *)param progress:(UploadProgress)uploadProgress success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    if (![Ability hasNet]) {
        
        [[[UIAlertView alloc]initWithTitle:NETTITLE message:NETCONTENT delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
        return;
    }
    
    urlString =[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPSessionManager * manager =[ SessionManager sharedManager];
    
    //因为是上传图片，所以这里用的是post请求
    [manager POST:urlString parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        for (NSInteger index =0; index < imageArray.count; index++) {
           
           //取出图片
            UIImage *image = (UIImage *)imageArray[index];
            //把图片转换成data数据
            NSData * imageData = UIImageJPEGRepresentation(image, 1);
            
            //做下面这一步的操作是为了防止图片名为空
            NSString * imageFileName =fileName;
            if (fileName==nil|| ![fileName isKindOfClass:[NSString class]] ||fileName.length==0) {
                
                NSDateFormatter * formatter =[[ NSDateFormatter alloc]init];
                formatter.dateFormat =@"yyyyMMddHHmmss";
                NSString * dateString =[ formatter stringFromDate:[NSDate date]];
                imageFileName =[NSString stringWithFormat:@"%@.png",dateString];
            }
            
            //取出需要上传的参数
            NSString *nameString =namesArray[index];
            
            [formData appendPartWithFileData:imageData name:nameString fileName:imageFileName mimeType:@"image/jpg"];
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        if (success) {
            
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        failure(error);
        
    }];
    
}

@end
